//
// Created by 李传智 on 2018/4/15.
//

#include "IPlayerProxy.h"
#include "FFPlayerBuilder.h"

void IPlayerProxy::init(void *vm) {
    mutex.lock();
    FFPlayerBuilder::get()->initHard(vm);
    if (!player) {
        player = FFPlayerBuilder::get()->buildPlayer();
    }
    mutex.unlock();
}

IPlayerProxy *IPlayerProxy::get() {
    static IPlayerProxy playerProxy;
    return &playerProxy;
}

bool IPlayerProxy::setDataSource(const char *url) {
    bool ret = false;
    mutex.lock();
    if (player) {
        ret = player->setDataSource(url);
    }
    mutex.unlock();
    return ret;
}

void IPlayerProxy::setPause(bool isPause) {
    mutex.lock();
    if (player) {
        player->setPause(isPause);
    }
    mutex.unlock();
}

bool IPlayerProxy::isPaused() {
    bool ret;
    mutex.lock();
    ret = player->isPaused();
    mutex.unlock();
    return ret;
}

bool IPlayerProxy::start() {
    bool ret = false;
    mutex.lock();
    if (player) {
        ret = player->start();
    }
    mutex.unlock();
    return ret;
}

void IPlayerProxy::close() {
    mutex.lock();
    if (player) {
        player->close();
    }
    mutex.unlock();
}

void IPlayerProxy::setRender(void *window) {
    mutex.lock();
    if (player) {
        player->setRender(window);
    }
    mutex.unlock();
}

bool IPlayerProxy::seek(double position) {
    bool ret = false;
    mutex.lock();
    if (player) {
        ret = player->seek(position);
    }
    mutex.unlock();
    return ret;
}

double IPlayerProxy::getCurrentPosition() {
    double position = 0;
    mutex.lock();
    if(player) {
        position = player->getCurrentPosition();
    }
    mutex.unlock();
    return position;
}

bool IPlayerProxy::startRecord(const char *url) {
    bool ret = false;
    mutex.lock();
    if (player) {
        ret = player->startRecord(url);
    }
    mutex.unlock();
    return ret;
}

bool IPlayerProxy::stopRecord() {
    bool ret = false;
    mutex.lock();
    if (player) {
        ret = player->stopRecord();
    }
    mutex.unlock();
    return ret;
}